using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TVDesigner.Entities.Elements;
using TVDesigner.Entities;


namespace TVDesigner.GUI
{

    public enum EConfigurationType
    {
        New,
        Open,
    }

    public partial class FConfiguration : Form
    {
        private FMain _MainForm;
        private EConfigurationType _Type;
        private GenerateInstructions _GenerateInstructions;

        public GenerateInstructions GenerateInstructions
        {
            get { return _GenerateInstructions; }
            set { _GenerateInstructions = value; }
        }

        public FMain MainForm
        {
            get { return _MainForm; }
            set { _MainForm = value; }
        }

        public FConfiguration(FMain mainForm)
        {
            _MainForm = mainForm;
            InitializeComponent();
            this._ChkIsSystemvariables.Checked = true;
            this._RdoSepareteFolders.Checked = true;
        }

        #region Public Methods
        public DialogResult ShowDialog(EConfigurationType configurationType)
        {
            _Type = configurationType;

            if (_Type == EConfigurationType.Open)
            {
                this.Text = "configurations";
                _LoadFields();
            }
            else
            {
                this.Text = "New Project";
            }
            return this.ShowDialog();
        }
        #endregion

        #region Private Methods

        private void _LoadFields()
        {
            GenerateInstructions instructions = _MainForm.Project.GenerateInstructions;

            _TxtProjectName.Text = instructions.ProjectName;
            _RdoSameFolder.Checked = instructions.SameFolder;
            _ChkIsSystemvariables.Checked = instructions.HasSystemVariables;

            if (!_ChkIsSystemvariables.Checked)
            {
                _TxtJavaLocation.Text = instructions.JavacLocation;
            }
        }

        private void _ClearErros()
        {
            _LblProjectName.ForeColor = Color.Black;
            _TxtProjectName.ForeColor = Color.Black;

            _LblJavaLocation.ForeColor = Color.Black;
            _TxtJavaLocation.ForeColor = Color.Black;

            defaultStatusBar1.DisplayIdleMessage("");
        }

        private bool _ValidateFields()
        {
            bool result = true;

            string projectName = _TxtProjectName.Text;
            if (projectName == null || projectName.Length == 0)
            {
                _LblProjectName.ForeColor = Color.Red;
                _TxtProjectName.ForeColor = Color.Red;

                result = false;
            }

            if (_TxtJavaLocation.Text.Length <= 0 && !_ChkIsSystemvariables.Checked)
            {
                _LblJavaLocation.ForeColor = Color.Red;
                _TxtJavaLocation.ForeColor = Color.Red; 

                result = false;
            }               

            return result;
        }

        private void _BtnJavaLocation_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == openFileDialogJava.ShowDialog())
            {
                this._TxtJavaLocation.Text = openFileDialogJava.FileName;
            }
        }

        private void _ButtonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void _ChkIsSystemvariables_CheckedChanged(object sender, EventArgs e)
        {
            if (this._ChkIsSystemvariables.Checked)
            {
                this._LblJavaLocation.Enabled = false;
                this._TxtJavaLocation.Enabled = false;
                this._BtnJavaLocation.Enabled = false;
            }
            else
            {
                this._LblJavaLocation.Enabled = true;
                this._TxtJavaLocation.Enabled = true;
                this._BtnJavaLocation.Enabled = true;
            }
        } 

        private void _ButtonSave_Click(object sender, EventArgs e)
        {
            _ClearErros();
            if (_ValidateFields())
            {
                if (_Type == EConfigurationType.New)
                {
                    _MainForm.Project = new ProjectXlet();
                    _GenerateInstructions = _MainForm.Project.GenerateInstructions;
                }

                _GenerateInstructions.ProjectName = _TxtProjectName.Text;
                _GenerateInstructions.SameFolder = _RdoSameFolder.Checked;
                _GenerateInstructions.HasSystemVariables = _ChkIsSystemvariables.Checked;
                _GenerateInstructions.JavacLocation = _TxtJavaLocation.Text;

                this.DialogResult = DialogResult.OK;
            }
            else
            {
                defaultStatusBar1.DisplayErrorMessage("Ajuste o valor do(s) do(s) campo(s) em vermelho.");
            }
        }

        private void _TxtProjectName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                _ButtonSave_Click(null, null);
            }
            else if (e.KeyCode == Keys.Cancel)
            {
                _ButtonCancel_Click(null, null);
            }
        }
        #endregion



    }
}